<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
   <div class="layui-fluid">
      <table class="layui-hide" id="depInfo" lay-filter="depInfo"></table>
  </div>
<script id="toolbar" type="text/html">
  <div class="layui-btn-container">
   
    <button class="layui-btn layui-btn-sm" lay-event="add">添加部门</button>
 </div>
</script>

<script id="bar" type="text/html">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="editLayer">
    <!-- 编辑模态框 -->
    <div>
     <form action="/editDep" class="layui-form" id="editForm">
       <div class="layui-form-item" style="display:none">
       <label class="layui-form-label">id</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="id" id="id" readonly="readonly">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">部门号</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="depNo" id="depNo">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">部门名称</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="depName" id="depName">
       </div>   
     </div>
     </form> 
    </div>
</script>

<script type="text/html" id="addLayer">
    <!-- 添加模态框 -->
     <div>
     <form action="/super/edit" class="layui-form" id="editForm">

     <div class="layui-form-item">
       <label class="layui-form-label">部门号</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="depNo" id="depNo">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">部门名称</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="depName" id="depName">
       </div>   
     </div>
     </form> 
    </div>
</script>

<script src="layui/layui.js"></script> 

<script th:inline="none">
  layui.use('table',function(){
	  var table=layui.table;
	  var $=layui.jquery;
	  table.render({
		 elem:'#depInfo',
		 url:'/getDepInfo',
		 method:'post',
	     toolbar:'#toolbar',
	     defaultToolbar:['filter','exports','print',{title:'提示',layEvent:'LAYTABLE_TIPS',icon:'layui-icon-tips'}],
	     title:'部门表',
	   //  response:{statusCode:200},
	     cols:[[
	    	 {type:'checkbox',fixed:'left'},
	    	 {field:'id',title:'ID',fixed:'left',unresize:true,sort:true},
	    	 {field:'depNo',title:'部门号',sort:true},
	    	// {field:'email', title:'邮箱', width:150, edit: 'text', templet: function(res){
	    	//        return '<em>'+ res.email +'</em>'
	    	 //     }},
	    	 {field:'depName', title:'部门名称',sort: true}
	    	 ,{field:'empNum', title:'部门人数'},
	    	 //,{field:'permission', title:'权限'},
	    	// ,{field:'experience', title:'积分', width:80, sort: true}
	    	 //,{field:'ip', title:'IP', width:120}
	    	 //,{field:'logins', title:'登入次数', width:100, sort: true}
	    	 //,{field:'joinTime', title:'加入时间', width:120}
	    	 {fixed: 'right', title:'操作', toolbar: '#bar'}
	     ]],
	     page:true
	     
	  });
	  
	  table.on('toolbar(depInfo)',function(obj){
		  var checkStatus=table.checkStatus(obj.config.id);
		  switch(obj.event){
		  case 'add':
			 // var data=checkStatus.data;
			//  layer.alert(JSON.stringify(data));
			  layui.use('form',function(){
				  var form =layui.form;
				  var $=layui.jquery;
				//  var id1=$("#id").val();
				//  var account=$("#account").val();
				
				  var index=layer.open({
					  formType:1,
					  title:'添加部门',
					  area:['400px','250px'],
					  content:$('#addLayer').html(),
					  btn:['确认','取消'],
					  yes:function(index,layero){
						  var depNo=$("#depNo").val();
						  var depName=$("#depName").val();
						
						  $.ajax({
							  url:'/addDep',
							  method:'post',
							  contentType:"application/json;charset=utf-8",
							  data:JSON.stringify({depNo:depNo,depName:depName}),
							  success:function(data){
								  layer.msg("添加成功");
								  window.setTimeout(function(){window.location.reload();},500)
							  },
							  error:function(data){
								  layer.msg("添加失败");
								  layer.msg(account1);
							  }
						  });
					  },
					  btn2:function(index){layer.close(index);},
					  success:function(index,layero){
						  form.render();
					  },
				  });
			  });
		  break;
		  case 'getCheckLength':
			  var data=checkStatus.data;
			  layer.msg('选中了：'+data.length+'个');
		  break;
		  case 'isAll':
			  layer.msg(checkStatus.isAll?'全选':'未全选');
		  break;
		  case 'LAYTABLE_TIPS':
			  layer.alert('这是工具栏右侧自定义的一个图标按钮');
		  break;
		  };
	  });
	  
	  //监听行工具事件
	  table.on('tool(depInfo)',function(obj){
		  var data=obj.data;
		  if(obj.event==='del'){
			  layer.confirm('真的删除吗',function(index){
				// obj.del();
				 layer.close(index);
					 
				  //向服务器端发送删除指令
				  $.ajax({
					  url:'/delDep?id='+data.id,
					  method:'post',
					  async:false,
					  success:function(data){
						  if(data){
						  layer.msg("删除成功");
						  obj.del();
						  } 
						//  }else{layer.msg("删除失败");}
					  },
					  error:function(data){
						  if(data){
							  layer.msg("删除失败");
						  }
					  }
				  });
			  });
		  }else if(obj.event==='edit'){
			 layui.use('form',function(){
				 var form=layui.form;
				 var $=layui.jquery;
			 var editIndex=layer.open({
				  formType:1,
				  title:'修改信息',
				  btn:['确认','取消'],
				  yes:function(editIndex,layero){
					//获取表单的值，必须再次出获取，若在给弹窗赋值后再获取，则获取到的仍是原始数据，相当于加载了一个空的表单，然后弹窗赋值，然后获取表单的值
					 var id=$("#id").val();
				     var depNo=$("#depNo").val();
				     var depName=$("#depName").val();
				    // var userName=$("#userName").val();
				    // var permission=$("#permission").val();
					 // layer.msg(account);

					  //向后台发送修改请求
					  $.ajax({
						  url:'/editDep',
						  contentType:"application/json;charset=utf-8",
						  method:'post',
						  data:JSON.stringify({id:id,depNo:depNo,depName:depName}),
						  success:function(data){
							  layer.msg("修改成功");
							  obj.update({
								  depNo:depNo,
								  depName:depName
							  });
							 
						  },
						  error:function(data){layer.msg("修改失败");}
					  });
				  },
				  btn2:function(editIndex,layero){
					  layer.close(editIndex);
				  },
				//  end:function(){parent.reload();},
				  area:['400px','250px'],
				  content:$('#editLayer').html(),
				  success:function(layero,index){ 
					  //给个控件赋值
						 $("[name='id']").val(data.id);
						 $("[name='depNo']").val(data.depNo);
						 $("[name='depName']").val(data.depName);
						// $("[name='userName']").val(data.userName);
						// $("[name='permission']").val(data.permission);
						 form.render()},
				  });			
			      
			
			 
			 });
		  }
	  });
  });
  
</script>
   
</body>
</html>